from django.db import models
from tools.random_str import *


# Create your models here.

class Putin(models.Model):
    my_id = models.CharField(max_length=10, default=my_putin_id, primary_key=True, verbose_name='入库单号')
    date_joined = models.DateField(auto_now_add=True, verbose_name='创建时间')
    desc = models.CharField(max_length=100, blank=True, null=True, verbose_name='描述')

    class Meta:
        verbose_name = '入库单'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.my_id


class PutinReceipt(models.Model):
    putin = models.ForeignKey(Putin, on_delete=models.CASCADE, verbose_name='入库单')
    receipt_num = models.CharField(max_length=20, primary_key=True, verbose_name='收据单号')
    market = models.ForeignKey('goods.GoodsMarket', on_delete=models.CASCADE, verbose_name='商城')
    fee = models.DecimalField(default=0, max_digits=4, decimal_places=2, verbose_name='快递费')
    desc = models.CharField(max_length=100, blank=True, null=True, verbose_name='描述')

    class Meta:
        verbose_name = '送货单'
        verbose_name_plural = verbose_name

    def show_market_image(self):
        return self.market.show_image()

    show_market_image.short_description = "商城"

    def __str__(self):
        return self.receipt_num


class PutinReceiptList(models.Model):
    putin_receipt = models.ForeignKey(PutinReceipt, on_delete=models.CASCADE, verbose_name='送货单')
    order = models.CharField(max_length=3, verbose_name='序号')
    market_num = models.ForeignKey('goods.GoodsMarketId', on_delete=models.CASCADE, verbose_name='商城编号')
    brand = models.ForeignKey('goods.GoodsBrand', on_delete=models.CASCADE, verbose_name='品牌')
    brand_num = models.CharField(max_length=30, verbose_name='厂家型号')
    number = models.IntegerField(verbose_name='数量')
    money = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='金额')

    class Meta:
        verbose_name = '送货详单'
        verbose_name_plural = verbose_name

    def get_goods_image(self):
        return self.market_num.get_goods_image()

    def get_market_image(self):
        return self.putin_receipt.show_market_image()

    def show_brand_image(self):
        return self.brand.show_image()

    get_goods_image.short_description = "元件"
    get_market_image.short_description = "商城"
    show_brand_image.short_description = "品牌"

    def __str__(self):
        return self.order
